package zhihuilan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import zhihuilan.dto.MyStaffMsgRoomDto;
import zhihuilan.dto.MyStaffShopGoodsDto;
import zhihuilan.dto.PageDto;
import zhihuilan.entity.MyStaffMsg;
import zhihuilan.entity.MyStaffMsgRoom;
import zhihuilan.mapper.MyStaffMsgRoomMapper;
import zhihuilan.params.query.MyStaffMsgRoomQueryParam;
import zhihuilan.service.MyStaffMsgRoomService;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


@Service
public class MyStaffMsgRoomServiceImpl extends ServiceImpl<MyStaffMsgRoomMapper, MyStaffMsgRoom> implements MyStaffMsgRoomService {



    @Resource
    private MyStaffMsgRoomMapper myStaffMsgRoomMapper;
    /**
     * 查询用户消息信息
     *

     * @return 用户信息集合
     */
    @Override
    public Page<MyStaffMsgRoom> selectMyStaffMsgRoomList(MyStaffMsgRoomQueryParam myStaffMsgRoom){
//        MPJLambdaWrapper queryWrapper = new MPJLambdaWrapper<MyStaffMsgRoom>().setAlias("a");
//        queryWrapper.select("a.*");
//        queryWrapper.leftJoin("my_staff_msg b on a.room_id=b.room_id");
//        queryWrapper.eq("a.delete_status", 0);
//        if (!ObjectUtils.isEmpty(myStaffMsgRoom)) {
//            if (!ObjectUtils.isEmpty(myStaffMsgRoom.getStaffId())) {
//                queryWrapper.eq("a.staff_id", myStaffMsgRoom.getStaffId());
//            }
//        }
//        queryWrapper.groupBy("a.room_id");
//        queryWrapper.orderByDesc("top_status,update_time,create_time");
//        Page<Map<String,Object>> page  = myStaffMsgRoomMapper.selectMapsPage(new Page<>(myStaffMsgRoom.getPage(), myStaffMsgRoom.getPageSize()), queryWrapper);
//        List<MyStaffMsgRoom> list = new ArrayList<>();
//        for(Integer i=0;i<page.getRecords().size();i++){
//           Map map =  (Map) page.getRecords().get(i);
//           MyStaffMsgRoom myStaffMsgRoomDto = new MyStaffMsgRoom();
//              myStaffMsgRoomDto.setDeleteStatus( (int)map.get("delete_status"));
//            myStaffMsgRoomDto.setRoomId(map.get("room_id").toString());
//           myStaffMsgRoomDto.setUpdateTime(ObjectUtils.isEmpty(map.get("update_time"))?0:(long)map.get("update_time"));
//            myStaffMsgRoomDto.setUpdateUser(ObjectUtils.isEmpty(map.get("update_user"))?0:Integer.parseInt(map.get("update_user").toString()) );
//           myStaffMsgRoomDto.setCreateTime((long)map.get("create_time"));
//            myStaffMsgRoomDto.setCreateUser(Integer.parseInt(map.get("create_user").toString()));
//           myStaffMsgRoomDto.setSender((int)map.get("sender"));
//            myStaffMsgRoomDto.setStaffId((int)map.get("staff_id"));
//            myStaffMsgRoomDto.setRecipient((int)map.get("recipient"));
//            myStaffMsgRoomDto.setTopStatus(ObjectUtils.isEmpty(map.get("top_status"))?0:(int)map.get("top_status"));
//            myStaffMsgRoomDto.setId((int)map.get("id"));
//            list.add(myStaffMsgRoomDto);
//        }
//        Page<MyStaffMsgRoom> pageDto = new Page<>(myStaffMsgRoom.getPage(),  myStaffMsgRoom.getPageSize());
//        pageDto.setRecords(list);
        QueryWrapper<MyStaffMsgRoom> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(myStaffMsgRoom)) {
            if (!ObjectUtils.isEmpty(myStaffMsgRoom.getStaffId())) {
                queryWrapper.eq("staff_id", myStaffMsgRoom.getStaffId());
            }
        }
        queryWrapper.gt("update_time",0);
        queryWrapper.eq("delete_status",0);
        queryWrapper.orderByDesc("top_status","update_time");
        Page<MyStaffMsgRoom> page = new Page<>(myStaffMsgRoom.getPage(),  myStaffMsgRoom.getPageSize());
        return baseMapper.selectPage(page,queryWrapper);
        //return pageDto;
   }

    /**
     * 根据ID查询用户消息；
     * */
    @Override
    public MyStaffMsgRoom selectMyStaffMsgRoomById(Integer id){
        return baseMapper.selectById(id);
    }


    @Override
    public MyStaffMsgRoom selectMyStaffMsgRoom(MyStaffMsgRoomQueryParam myStaffMsgRoom){
        QueryWrapper<MyStaffMsgRoom> queryWrapper = new QueryWrapper<>();
        if(myStaffMsgRoom!=null&&myStaffMsgRoom.getRoomId()!=null) {
            queryWrapper.eq("room_id", myStaffMsgRoom.getRoomId());
        }
        if(myStaffMsgRoom.getStaffId()!=null&&myStaffMsgRoom.getStaffId()>0){
            queryWrapper.eq("staff_id",myStaffMsgRoom.getStaffId());

        }
        if(myStaffMsgRoom.getRecipient()!=null&&myStaffMsgRoom.getRecipient()>0){
        //    queryWrapper.nested(qw->qw.eq("recipient",myStaffMsgRoom.getRecipient()).or().eq("sender",myStaffMsgRoom.getRecipient()).or().eq("sender",myStaffMsgRoom.getStaffId()).or().eq("recipient",myStaffMsgRoom.getStaffId()));

            queryWrapper.nested(qw->qw.eq("recipient",myStaffMsgRoom.getRecipient()).or().eq("sender",myStaffMsgRoom.getRecipient()));
            queryWrapper.nested(qw->qw.eq("sender",myStaffMsgRoom.getStaffId()).or().eq("recipient",myStaffMsgRoom.getStaffId()));
        }
        queryWrapper.last("limit 1");
//        if(myStaffMsgRoom.getRecipient()!=null&&myStaffMsgRoom.getRecipient()>0){
//            queryWrapper.eq("staff_id",myStaffMsgRoom.getRecipient());
//        }
        try {
            MyStaffMsgRoom m =baseMapper.selectOne(queryWrapper);
            return m;
        }catch (Exception e){
            return null;
        }

    }
    /**
     * 删除消息
     *
     * */
    @Override
    public void delMyStaffMsgRoomById(String id){
        QueryWrapper<MyStaffMsgRoom> queryWrapper = new QueryWrapper<>();
        baseMapper.deleteById(id);
    }

    /**
     * 新增用户消息
     * */
    @Override
    public Integer addEntity(MyStaffMsgRoomQueryParam entity){
        MyStaffMsgRoom myStaffMsgRoom = new MyStaffMsgRoom();
        BeanUtils.copyProperties(entity,myStaffMsgRoom);
       return baseMapper.insert(myStaffMsgRoom);
    }

    /**
     * 更新用户消息
     * */
    @Override
    public  Integer updateEntity(MyStaffMsgRoomQueryParam entity){
            QueryWrapper<MyStaffMsgRoom> queryWrapper = new QueryWrapper<>();
            if(!entity.getRoomId().isEmpty()) {
                queryWrapper.eq("room_id", entity.getRoomId());
            }
            if(entity.getStaffId()!=null&&entity.getStaffId()>0){
                queryWrapper.eq("staff_id",entity.getStaffId());
            }
            MyStaffMsgRoom msgRoom = new MyStaffMsgRoom();
            msgRoom.setDeleteStatus(0);
            msgRoom.setUpdateTime(new Date().getTime()/1000);
            msgRoom.setUpdateUser(entity.getUpdateUser());
            MyStaffMsgRoom myStaffMsgRoom = new MyStaffMsgRoom();
            BeanUtils.copyProperties(entity,myStaffMsgRoom);
            return  baseMapper.update(myStaffMsgRoom,queryWrapper);
    }

    @Override
    public   Integer updateDeleteStatus(MyStaffMsgRoomQueryParam entity){
        QueryWrapper<MyStaffMsgRoom> queryWrapper = new QueryWrapper<>();
        if(entity.getStaffId()>0){
            queryWrapper.eq("staff_id",entity.getStaffId());
        }
        if(entity.getRecipient()!=null&&entity.getRecipient()>0){
            queryWrapper.nested(qw->qw.eq("recipient",entity.getRecipient()).or().eq("sender",entity.getRecipient()));
            queryWrapper.nested(qw->qw.eq("sender",entity.getStaffId()).or().eq("recipient",entity.getStaffId()));
         //   queryWrapper.nested(qw->qw.eq("recipient",entity.getRecipient()).or().eq("sender",entity.getRecipient()).or().eq("sender",entity.getStaffId()).or().eq("recipient",entity.getStaffId()));
       //     queryWrapper.or().eq("recipient",entity.getRecipient()).or().eq("sender",entity.getRecipient()).or().eq("sender",entity.getStaffId()).or().eq("recipient",entity.getStaffId());
        }
        MyStaffMsgRoom msgRoom = new MyStaffMsgRoom();
        msgRoom.setDeleteStatus(0);
        return baseMapper.update(msgRoom,queryWrapper);
    }

    @Override
    public   Integer updateDelete(MyStaffMsgRoomQueryParam entity){
        QueryWrapper<MyStaffMsgRoom> queryWrapper = new QueryWrapper<>();
        if(entity.getStaffId()>0){
            queryWrapper.eq("staff_id",entity.getStaffId());
        }
        if(entity.getRoomId()!=null){
            queryWrapper.eq("room_id",entity.getRoomId());
        }
        MyStaffMsgRoom msgRoom = new MyStaffMsgRoom();
        msgRoom.setDeleteStatus(1);
        msgRoom.setTopStatus(0);
        return baseMapper.update(msgRoom,queryWrapper);
    }

    @Override
    public  Integer updateTopStatus(MyStaffMsgRoomQueryParam entity){
        QueryWrapper<MyStaffMsgRoom> queryWrapper = new QueryWrapper<>();
        if(entity.getStaffId()>0){
            queryWrapper.eq("staff_id",entity.getStaffId());
        }
        if(entity.getRoomId()!=null){
            queryWrapper.eq("room_id",entity.getRoomId());
        }
        MyStaffMsgRoom myStaffMsgRoom = new MyStaffMsgRoom();
        myStaffMsgRoom.setTopStatus(entity.getTopStatus());
        return baseMapper.update(myStaffMsgRoom,queryWrapper);
    }
}
